<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AnswerController extends Controller
{
   // 回帖控制器
    public function getAnswerlist($id){
    	// echo '222';die;
        // 设定帖子id为1
        // $id = $id;
        $data = DB::table('tiezi')->join('user','user.id','=','tiezi.uid')->where('tiezi.id',$id)->first();
        $rely = DB::table('answer')->join('user','user.id','=','answer.uid')->where('answer.tid',$id)->paginate(3);
        // dd($data);
        // dd($rely);
        //显示分页
        // $answer = DB::table('answer')
        // ->where('tid',$id)
        // ->paginate(3);
        // dd($answer);
        $menu = HomeListController::menu(0);
    	return view('home.answer.answer',['menu'=>$menu,'data'=>$data,'rely'=>$rely,'tid'=>$id]);
    }

    public function postAinsert(Request $request){


        //判断用户是否登录
        if(session('id')){
            $data = $request->only(['content','tid']);
            // dd($data);
            $uid = session('id');
            // dd($uid);
            $atime =date('Y-m-d H:i:s', time());
            // dd($atime);
            // dd($data);
            $data['uid']=$uid;
            $data['atime'] = $atime;
            // dd($data);
            $res = DB::table('answer')->insert($data);
            // dd($i);
            if($res){
                // return redirect('/answer/answerlist',['id'=>$data['tid']]);
                return redirect()->action('Answercontroller@getAnswerlist', ['id'=>$data['tid']]);
            }
        }else{
            return redirect('/login');; 

        }

    }
    public function getList(){
        $data = DB::table('answer')->join('tiezi','answer.tid','=','tiezi.id')->join('user','user.id','=','answer.uid')
        ->select('answer.*','tiezi.title','user.username')->paginate(5);
        // dd($data);
        return view('admin.answer.index',['data'=>$data]);
    }


    public function getDanswer(Request $request){
        $id = $request->input('id');
        $res = DB::table('answer')->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
}
